package com.ruoyi.web.controller.pollution;

import com.alibaba.fastjson2.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.excel.ExcelUtiles;
import com.ruoyi.pollution.domain.PollutionAnalyseVo;
import com.ruoyi.pollution.service.PollutionAnalyseService;
import dm.jdbc.plugin.fldr.ColumnData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RestController
@RequestMapping("/pollution")
public class PollutionAnalyseController extends BaseController {

    @Autowired
    private PollutionAnalyseService pollutionAnalyseService;

    /**
     * 污染源树
     */
    @GetMapping("/dataSourcePolluteTree")
    public AjaxResult dataSourcePolluteTree(PollutionAnalyseVo vo) {
        return AjaxResult.success(pollutionAnalyseService.dataSourcePolluteTree(vo));
    }

    /**
     * 监测因子列表
     *
     * @param vo
     * @return
     */

    @GetMapping("/dataAnalysis")
    public AjaxResult dataAnalysis(PollutionAnalyseVo vo) {

        return AjaxResult.success(pollutionAnalyseService.dataAnalysis(vo));
    }


    @GetMapping("/dataAnalysisChart")
    public AjaxResult dataAnalysisChart(PollutionAnalyseVo vo) {

        return AjaxResult.success(pollutionAnalyseService.dataAnalysisChart(vo));
    }

    /**
     * 污染源企业下拉-行政区划
     */
    @GetMapping("/polluteTree")
    public AjaxResult polluteTree(PollutionAnalyseVo vo) {
        vo.setType("county");
        return AjaxResult.success(pollutionAnalyseService.getPolluteTree(vo));
    }

    /**
     * 企业关联分析
     */
    @PostMapping("/enterpriseRelation")
    public TableDataInfo enterpriseRelation(@RequestBody PollutionAnalyseVo vo) {
        startPage();
        List<Map<String,Object>> dataList =pollutionAnalyseService.enterpriseRelation(vo);
        return getDataTable(dataList);
    }

    /**
     * 统计监测因子
     */
    @GetMapping("/statisticMonitorFactor")
    public AjaxResult statisticMonitorFactor(PollutionAnalyseVo vo){
        List<Map<String,Object>> list =pollutionAnalyseService.statisticMonitorFactor(vo);
        return AjaxResult.success(list);
    }

    /**
     * 监测情况统计
     */
    @GetMapping("/statisticMonitorRate")
    public TableDataInfo statisticMonitorRate(PollutionAnalyseVo vo){
        startPage();
        List<Map<String,Object>> list =pollutionAnalyseService.statisticMonitorRate(vo);
        return getDataTable(list);
    }


    /**
     * 重点区域综合分析
     */
    @GetMapping("/enterpriseAnalyse")
    public AjaxResult enterpriseAnalyse(PollutionAnalyseVo vo){
        return AjaxResult.success(pollutionAnalyseService.enterpriseAnalyse(vo));
    }


    /**
     * 污染源类别分析
     */
    @GetMapping("/polluteTypeAnalyse")
    public AjaxResult polluteTypeAnalyse(PollutionAnalyseVo vo){
        return AjaxResult.success(pollutionAnalyseService.polluteTypeAnalyse(vo));
    }

    /**
     * 报表分析 日报
     */
    @GetMapping("/reportAnalyseDay")
    public AjaxResult reportAnalyseDay(PollutionAnalyseVo vo){
        return AjaxResult.success(pollutionAnalyseService.reportAnalyseDay(vo));
    }

    /**
     * 报表分析 日报导出
     */
    @GetMapping("/reportAnalyseDayExport")
    public void reportAnalyseDayExport(HttpServletResponse response, PollutionAnalyseVo vo) throws IOException {
        Map<String,Object> dataMap = pollutionAnalyseService.reportAnalyseDay(vo);
        Map<String,String> retMap=new LinkedHashMap<>();
        retMap.put("时间","时间");
        Map<String,Object> columnTitle = (Map<String, Object>) dataMap.get("columnTitle");
        for(Map.Entry<String,Object> entry:columnTitle.entrySet()){
            retMap.put(String.valueOf(entry.getValue()), String.valueOf(entry.getValue()));
        }
        List<Map<String,Object>> dataList = mapToList(dataMap);
        ExcelUtiles.exportMapToExcel(response, dataList,retMap,"日报报表"+ DateUtils.dateTime(),"sheet1", dataMap.get("PolluteName")+"日报报表");
    }

    /**
     * 报表分析 周报
     */
    @GetMapping("/reportAnalyseWeek")
    public AjaxResult reportAnalyseWeek(PollutionAnalyseVo vo){
        return AjaxResult.success(pollutionAnalyseService.reportAnalyseWeek(vo));
    }
    /**
     * 报表分析 周报导出
     */
    @GetMapping("/reportAnalyseWeekExport")
    public void reportAnalyseWeekExport(HttpServletResponse response, PollutionAnalyseVo vo) throws IOException {
        Map<String,Object> dataMap = pollutionAnalyseService.reportAnalyseWeek(vo);
//        List<Map<String,Object>> dataList = mapToList(dataMap);
//        ExcelUtiles.exportExcel( dataList, "周报报表"+ DateUtils.dateTime(),response);
        Map<String,String> retMap=new LinkedHashMap<>();
        retMap.put("时间","时间");
        Map<String,Object> columnTitle = (Map<String, Object>) dataMap.get("columns");
        for(Map.Entry<String,Object> entry:columnTitle.entrySet()){
            retMap.put(String.valueOf(entry.getValue()), String.valueOf(entry.getValue()));
        }
        List<Map<String,Object>> dataList = mapToListV2(dataMap);
        ExcelUtiles.exportMapToExcel(response, dataList,retMap,"周报报表"+ DateUtils.dateTime(),"sheet1", dataMap.get("PolluteName")+"周报报表");

    }

    /**
     * 报表分析 月报
     */
    @GetMapping("/reportAnalyseMonth")
    public AjaxResult reportAnalyseMonth(PollutionAnalyseVo vo){
        return AjaxResult.success(pollutionAnalyseService.reportAnalyseWeek(vo));
    }
    /**
     * 报表分析 月报导出
     */
    @GetMapping("/reportAnalyseMonthExport")
    public void reportAnalyseMonthExport(HttpServletResponse response, PollutionAnalyseVo vo) throws IOException {
        Map<String,Object> dataMap = pollutionAnalyseService.reportAnalyseWeek(vo);
//        List<Map<String,Object>> dataList = mapToList(dataMap);
//        ExcelUtiles.exportExcel( dataList, "月报报表"+ DateUtils.dateTime(),response);
        Map<String,String> retMap=new LinkedHashMap<>();
        retMap.put("时间","时间");
        Map<String,Object> columnTitle = (Map<String, Object>) dataMap.get("columns");
        for(Map.Entry<String,Object> entry:columnTitle.entrySet()){
            retMap.put(String.valueOf(entry.getValue()), String.valueOf(entry.getValue()));
        }
        List<Map<String,Object>> dataList = mapToListV2(dataMap);
        ExcelUtiles.exportMapToExcel(response, dataList,retMap,"月报报表"+ DateUtils.dateTime(),"sheet1", dataMap.get("PolluteName")+"月报报表");

    }

    /**
     * 报表分析 季度
     */
    @GetMapping("/reportAnalyseQuarter")
    public AjaxResult reportAnalyseQuarter(PollutionAnalyseVo vo){
        return AjaxResult.success(pollutionAnalyseService.reportAnalyseQuarter(vo));
    }
    /**
     * 报表分析 季度导出
     */
    @GetMapping("/reportAnalyseQuarterExport")
    public void reportAnalyseQuarterExport(HttpServletResponse response, PollutionAnalyseVo vo) throws IOException {
        Map<String,Object> dataMap = pollutionAnalyseService.reportAnalyseQuarter(vo);
//        List<Map<String,Object>> dataList = mapToList(dataMap);
//        ExcelUtiles.exportExcel( dataList, "季度报表"+ DateUtils.dateTime(),response);
        Map<String,String> retMap=new LinkedHashMap<>();
        retMap.put("时间","时间");
        Map<String,Object> columnTitle = (Map<String, Object>) dataMap.get("columns");
        for(Map.Entry<String,Object> entry:columnTitle.entrySet()){
            retMap.put(String.valueOf(entry.getValue()), String.valueOf(entry.getValue()));
        }
        List<Map<String,Object>> dataList = mapToListV2(dataMap);
        ExcelUtiles.exportMapToExcel(response, dataList,retMap,"季度报表"+ DateUtils.dateTime(),"sheet1", dataMap.get("PolluteName")+"季度报表");

    }

    /**
     * 报表分析 半年
     */
    @GetMapping("/reportAnalyseSemiannual")
    public AjaxResult reportAnalyseSemiannual(PollutionAnalyseVo vo){
        return AjaxResult.success(pollutionAnalyseService.reportAnalyseQuarter(vo));
    }
    /**
     * 报表分析 半年导出
     */
    @GetMapping("/reportAnalyseSemiannualExport")
    public void reportAnalyseSemiannualExport(HttpServletResponse response, PollutionAnalyseVo vo) throws IOException {
        Map<String,Object> dataMap = pollutionAnalyseService.reportAnalyseQuarter(vo);
//        List<Map<String,Object>> dataList = mapToList(dataMap);
//        ExcelUtiles.exportExcel( dataList, "半年报表"+ DateUtils.dateTime(),response);
        Map<String,String> retMap=new LinkedHashMap<>();
        retMap.put("时间","时间");
        Map<String,Object> columnTitle = (Map<String, Object>) dataMap.get("columns");
        for(Map.Entry<String,Object> entry:columnTitle.entrySet()){
            retMap.put(String.valueOf(entry.getValue()), String.valueOf(entry.getValue()));
        }
        List<Map<String,Object>> dataList = mapToListV2(dataMap);
        ExcelUtiles.exportMapToExcel(response, dataList,retMap,"半年报表"+ DateUtils.dateTime(),"sheet1", dataMap.get("PolluteName")+"半年报表");

    }

    /**
     * 报表分析 年度
     */
    @GetMapping("/reportAnalyseYear")
    public AjaxResult reportAnalyseYear(PollutionAnalyseVo vo){
        return AjaxResult.success(pollutionAnalyseService.reportAnalyseQuarter(vo));
    }
    /**
     * 报表分析 年度导出
     */
    @GetMapping("/reportAnalyseYearExport")
    public void reportAnalyseYearExport(HttpServletResponse response, PollutionAnalyseVo vo) throws IOException {
        Map<String,Object> dataMap = pollutionAnalyseService.reportAnalyseQuarter(vo);
//        List<Map<String,Object>> dataList = mapToList(dataMap);
//        ExcelUtiles.exportExcel( dataList, "年度报表"+ DateUtils.dateTime(),response);
        Map<String,String> retMap=new LinkedHashMap<>();
        retMap.put("时间","时间");
        Map<String,Object> columnTitle = (Map<String, Object>) dataMap.get("columns");
        for(Map.Entry<String,Object> entry:columnTitle.entrySet()){
            retMap.put(String.valueOf(entry.getValue()), String.valueOf(entry.getValue()));
        }
        List<Map<String,Object>> dataList = mapToListV2(dataMap);
        ExcelUtiles.exportMapToExcel(response, dataList,retMap,"年度报表"+ DateUtils.dateTime(),"sheet1", dataMap.get("PolluteName")+"年度报表");

    }


    private List<Map<String,Object>> mapToList(Map<String,Object> dataMap){
        List<Map<String,Object>> retList =new ArrayList<>();
        Map<String,Object> columnTitle= (Map<String, Object>) dataMap.get("columnTitle");
        List<Map<String,Object>> dataList = (List<Map<String,Object>>)dataMap.get("dataList");
        Map<String,Object> sum = (Map<String,Object>)dataMap.get("sum");
        Map<String,Object> avg = (Map<String,Object>)dataMap.get("avg");
        Map<String,Object> min = (Map<String,Object>)dataMap.get("min");
        Map<String,Object> max = (Map<String,Object>)dataMap.get("max");
        min.put("monitorTime","最小值");
        max.put("monitorTime","最大值");
        avg.put("monitorTime","平均值");
        sum.put("monitorTime","合计");
        dataList.add(min);
        dataList.add(max);
        dataList.add(avg);
        dataList.add(sum);

        for(Map<String,Object> map:dataList){
            Map<String,Object> retMap = new LinkedHashMap<>();
            retMap.put("时间", map.get("monitorTime"));
            for(Map.Entry<String,Object> entry:columnTitle.entrySet()){
                retMap.put(String.valueOf(entry.getValue()), map.get(entry.getKey()));
            }
            retList.add(retMap);
        }
        return retList;
    }


    private List<Map<String,Object>> mapToListV2(Map<String,Object> dataMap){
        List<Map<String,Object>> retList =new ArrayList<>();
        Map<String,Object> columnTitle= (Map<String, Object>) dataMap.get("columns");
        List<Map<String,Object>> dataList = (List<Map<String,Object>>)dataMap.get("list");
        Map<String,Object> sum = (Map<String,Object>)dataMap.get("sum");
        Map<String,Object> avg = (Map<String,Object>)dataMap.get("avg");
        Map<String,Object> min = (Map<String,Object>)dataMap.get("min");
        Map<String,Object> max = (Map<String,Object>)dataMap.get("max");
        min.put("monitorTime","最小值");
        max.put("monitorTime","最大值");
        avg.put("monitorTime","平均值");
        sum.put("monitorTime","合计");
        dataList.add(min);
        dataList.add(max);
        dataList.add(avg);
        dataList.add(sum);

        for(Map<String,Object> map:dataList){
            Map<String,Object> retMap = new LinkedHashMap<>();
            retMap.put("时间", map.get("monitorTime"));
            for(Map.Entry<String,Object> entry:columnTitle.entrySet()){
                retMap.put(String.valueOf(entry.getValue()), map.get(entry.getKey()));
            }
            retList.add(retMap);
        }
        return retList;
    }
}
